package com.mathdroid.gui.profile;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;

import com.mathdroid.gui.ImageLoader;
import com.mathdroid.gui.main.DroidComboBoxModel;
import com.mathdroid.gui.main.Popup;
import com.mathdroid.model.Model;
import com.mathdroid.model.Profile;
import com.mathdroid.model.Profiles;
import com.mathdroid.model.enums.ESpec;
import com.mathdroid.persistence.Serializer;

/**
 * A popup like frame to create a new profile.
 */
public class NewProfile extends Popup {
	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Textfield for the name.
	 */
	private final JTextField nameField = new JTextField();
	
	/**
	 * Combobox to select a spec.
	 */
	private JComboBox spec = null;
	
	/**
	 * Spacing between components.
	 */
	private static final int SPACING = 10;
	
	/**
	 * The image loader.
	 */
	private final ImageLoader il = new ImageLoader();

	/**
	 * Create a new "New Profile" popup.
	 * @param parent The parent UI component to align to.
	 */
	public NewProfile(final Component parent) {
		super(parent);
		
		buildTextField();
		buildComboBox();
		buildCreateButton();
		buildCloseButton();
		
		this.pack();
		this.setVisible(true);
	}

	/**
	 * Build the "Close" button.
	 */
	private void buildCloseButton() {
		this.getConstraints().gridx = 1;
		this.getConstraints().insets = new Insets(0, SPACING, SPACING, SPACING);
		JButton close = new JButton("Close");
		close.setIcon(this.il.getImage("cross"));
		close.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				dispose();
			}
		});
		this.getPanel().add(close, this.getConstraints());
	}

	/**
	 * Build the "Create" button.
	 */
	private void buildCreateButton() {
		this.getConstraints().gridx = 0;
		this.getConstraints().gridy = 2;
		this.getConstraints().gridwidth = 1;
		this.getConstraints().insets = new Insets(0, SPACING, SPACING, SPACING);
		JButton create = new JButton("Create");
		create.setIcon(this.il.getImage("add"));
		create.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				String name = nameField.getText();
				Serializer s = new Serializer();
				Profiles profiles = s.getProfiles();
				boolean valid = true;
				
				if (name.equals("")) {
					valid = false;
					nameField.setText("Please insert a name.");
				}
				if (profiles.get(name) != null) {
						valid = false;
				}
				if (valid) {
					Profile newProfile = new Profile(
							name, (ESpec) spec.getSelectedItem());
					profiles.put(name, newProfile);
					s.serialize(profiles);
					Model.setProfile(newProfile);
					dispose();
				}
				if (!valid) {
					nameField.setForeground(Color.RED);
				}
			}
		});
		this.getPanel().add(create, this.getConstraints());
	}

	/**
	 * Build the combobox to select a spec.
	 */
	private void buildComboBox() {
		DroidComboBoxModel model = new DroidComboBoxModel();
		for (ESpec s : ESpec.getAllSpecs()) {
			model.addElement(s);
		}
		this.spec = new JComboBox(model);
		
		this.getConstraints().gridx = 0;
		this.getConstraints().gridy = 1;
		this.getConstraints().insets = new Insets(0, SPACING, SPACING, SPACING);
		spec.setSelectedIndex(0);
		this.getPanel().add(spec, this.getConstraints());
	}

	/**
	 * Build the textfield for the profile/character name.
	 */
	private void buildTextField() {
		nameField.setBackground(this.getPanel().getBackground());
		nameField.setBorder(BorderFactory.createTitledBorder("Profile name"));
		this.getConstraints().gridx = 0;
		this.getConstraints().gridy = 0;
		this.getConstraints().gridwidth = 2;
		this.getConstraints().insets = new Insets(
				SPACING, SPACING, SPACING, SPACING);
		this.getConstraints().fill = GridBagConstraints.HORIZONTAL;
		this.getPanel().add(nameField, this.getConstraints());
	}
}
